﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlDataAccess;

namespace QuanLyBanHang
{
    public partial class F820_Tra_cuu_nhan_vien : Form
    {
        public F820_Tra_cuu_nhan_vien()
        {
            InitializeComponent();
            set_define_events();
        }
        #region Members
        BKIQuanLyBanHangEntities db = new BKIQuanLyBanHangEntities();
        #endregion

        #region Private Methods

        private void set_define_events()
        {
            this.Load += F820_Tra_cuu_nhan_vien_Load;
            this.m_txt_tu_khoa.TextChanged += m_txt_tu_khoa_TextChanged;
            this.m_grv.Click += m_grv_Click;
        }

        private void load_data_to_grid()
        {
            var v_lst_nhan_vien = db.HT_NGUOI_SU_DUNG
                .Where(x => x.MA_NHAN_VIEN.Contains(m_txt_tu_khoa.Text)
                || x.HO_TEN_NHAN_VIEN.Contains(m_txt_tu_khoa.Text)
                || m_txt_tu_khoa.Text.Equals("")
                && (x.ID_NHOM_NGUOI_DUNG == (decimal)1))
                .OrderBy(x => x.MA_NHAN_VIEN)
                .Select(x => new
                {
                    ID = x.ID,
                    MA_NHAn_VIEN = x.MA_NHAN_VIEN,
                    TEN_NHAN_VIEN = x.HO_TEN_NHAN_VIEN,
                    NGAY_SINH = x.NGAY_SINH,
                    GIO_TINH = x.GIOI_TINH,
                    DIA_CHI = x.DIA_CHI,
                    DIEN_THOAI = x.DIEN_THOAI,
                    EMAIL = x.EMAIL,
                    FACEBOOK = x.FACEBOOK
                })
                .ToList();
            m_grv.DataSource = v_lst_nhan_vien;

            m_grv.Columns[0].HeaderText = "ID";
            m_grv.Columns[0].Visible = false;
            m_grv.Columns[1].HeaderText = "Mã NV";
            m_grv.Columns[1].Width = 50;
            m_grv.Columns[2].HeaderText = "Họ tên";
            m_grv.Columns[2].Width = 100;
            m_grv.Columns[3].HeaderText = "Ngày sinh";
            m_grv.Columns[3].Width = 50;
            m_grv.Columns[4].HeaderText = "Giới tính";
            m_grv.Columns[4].Width = 30;
            m_grv.Columns[5].HeaderText = "Địa chỉ";
            m_grv.Columns[5].Width = 200;
            m_grv.Columns[6].HeaderText = "Điện thoại";
            m_grv.Columns[6].Width = 50;
            m_grv.Columns[7].HeaderText = "Email";
            m_grv.Columns[7].Width = 50;
            m_grv.Columns[8].HeaderText = "FB";
            m_grv.Columns[8].Width = 30;
            m_grv.Columns[8].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }
        #endregion

        #region Events
        private void F820_Tra_cuu_nhan_vien_Load(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        void m_grv_Click(object sender, EventArgs e)
        {
            try
            {
                decimal id_nhan_vien = (decimal)m_grv.CurrentRow.Cells[0].Value;
                HT_NGUOI_SU_DUNG v_ht_nhan_vien = db.HT_NGUOI_SU_DUNG.FirstOrDefault(x => x.ID == id_nhan_vien); ;
                if (v_ht_nhan_vien != null)
                {
                    m_txt_ma_nhan_vien.Text = v_ht_nhan_vien.MA_NHAN_VIEN;
                    m_txt_ho_ten.Text = v_ht_nhan_vien.HO_TEN_NHAN_VIEN;
                    m_txt_ngay_sinh.Text = v_ht_nhan_vien.NGAY_SINH.ToString();
                    m_txt_tai_khoan.Text = v_ht_nhan_vien.USER_NAME;
                    m_txt_gioi_tinh.Text = v_ht_nhan_vien.GIOI_TINH;
                    m_txt_dia_chi.Text = v_ht_nhan_vien.DIA_CHI;
                    m_txt_dien_thoai.Text = v_ht_nhan_vien.DIEN_THOAI.ToString();
                    m_txt_email.Text = v_ht_nhan_vien.EMAIL;
                    m_txt_facebook.Text = v_ht_nhan_vien.FACEBOOK;
                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        void m_txt_tu_khoa_TextChanged(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_tim_kiem_tim_kiem_nhan_vien_Click(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_quay_lai_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        #endregion
    }
}
